package com.xbjtech.sms;

import com.xbjtech.FailSendException;
import com.xbjtech.util.SmsSend;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Iterator;
import java.util.Map;

@Component("smsSend")
public class SmsSendImpl implements SmsSend {

    public static final String TO_CAROWNER_FTL = "【蓝领安装】验证码${code}，尊敬的车主，您的订单已安排到如下安装店：${name}，预约电话：${phone}，地址：${address}。请提前一天致电安装店预约，完工后请自行检查安装情况。";
    public static final String TO_STORE_FTL = "【蓝领安装】订单号：${orderId}，已指派到您的门店：${orderInfo}，完工后在微信公众号“蓝领安装”提交验证码申请完工。";

    @Override
    public void send(String smsModelId, String mobiles, Map<String, String> model) throws FailSendException {
        if ("1".equals(smsModelId)) {
            System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
            System.out.println("短信接收方：" + mobiles);
            System.out.println("短信的内容：" + replaceSmsBody(TO_CAROWNER_FTL, model));
            System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
        } else if ("2".equals(smsModelId)) {
            System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
            System.out.println("短信接收方：" + mobiles);
            System.out.println("短信的内容：" + replaceSmsBody(TO_STORE_FTL, model));
            System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
        } else {
            System.err.println("模板参数有误！！！");
        }
    }

    public static String replaceSmsBody(String smsBody, Map<String, String> params) {
        Iterator<String> it = params.keySet().iterator();
        while (it.hasNext()) {
            String _key = it.next();
            String _value = params.get(_key);
            smsBody = smsBody.replace(("${" + _key + "}"), StringUtils.trimWhitespace(_value));
        }
        return smsBody;
    }
}
